/**
 * 前台首页 菜单 首页版本20170110
 * @type {{}}
 */
var menu={
    url:{
        urlGetMenuJson:function(){
            return rootPath+"/menuController/getUserMenuTree";
        }
    },
    handle:{
        handleFirstMenuJson:function(data){
            var menuHtml=$('<ul id="top-menu-js"><li class="nav-list"><a style="color: #fff;text-decoration: none;" href="'+rootPath+'/index">首页</a></li></ul>');
            for(var i=0;i<data.length;i++){
                var node = data[i];
                var icon = node.attributes.icon;
                var pid = node.pid;
                var id = node.id;
                var text = node.text;
                var state = node.state;
                var url = (node.attributes.url!=""&&node.attributes.url!=null&&node.attributes.url!="#")?node.attributes.url:"javascript:;";
                var childNode = data[i].children;
                var hasChild = (state=="0")?true:false;
                if(hasChild){
                    $('<li class="nav-list">'+text+'' +
                        '<div class="nav-2" data-pid="'+pid+'" data-id="'+id+'">' +
                            '<div class="my-icon-up fa fa-caret-up"></div>' +
                            '<ul style="overflow:hidden;" data-pid="'+pid+'" data-id="'+id+'"></ul>'+
                        '</div>'+
                    '</li>').appendTo(menuHtml);
                    menu.handle.handleSecondMenuJson(childNode,menuHtml);
                }else{
                    $('<li class="nav-list"><a href="'+rootPath+'/'+url+'" onclick="addMenuLog(\''+id+'\',\''+text+'\',\''+url+'\')" >'+text+'</a></li>').appendTo(menuHtml);
                }
            }
            $(".top-nav").html("");
            menuHtml.appendTo(".top-nav");
            menu.action.menuClick();
        },
        handleSecondMenuJson:function(nodes,parentNode){
            if (nodes.length != 0) {
                //判断资源不小于0
                for (var i = 0; i < nodes.length; i++) {
                    var node = nodes[i];
                    var icon = node.attributes.icon;
                    var pid = node.pid;
                    var id = node.id;
                    var text = node.text;
                    var state = node.state;
                    var url = (node.attributes.url!=""&&node.attributes.url!=null&&node.attributes.url!="#")?node.attributes.url:"javascript:;";
                    var childNode = node.children;
                    var hasChild = (state=="0")?true:false;
                    //判断是否是文件夹
                    if (hasChild) {
                        $('<div class="nav-3" data-pid="'+pid+'" data-id="'+id+'"><ul></ul></div>').appendTo(parentNode.find("div.nav-2[data-id='"+pid+"']"));
                        $('<li class="nav-2-list"  data-id="'+id+'">'+text+'</li>').appendTo(parentNode.find("ul[data-id='"+pid+"']"));
                        //如果有孩子
                        menu.handle.handleThirdMenuJson(childNode,parentNode);
                    } else {
                        $('<li class="nav-2-list" style="line-height:25px;" data-pid="'+pid+'" data-id="'+id+'"><a href="'+rootPath+'/'+url+'">'+text+'</a></li>').appendTo(parentNode.find("ul[data-id='"+pid+"']"));
                    }
                }
            }
        },
        handleThirdMenuJson:function(nodes,parentNode){
            if (nodes.length != 0) {
                //判断资源不小于0
                for (var i = 0; i < nodes.length; i++) {
                    var node = nodes[i];
                    var icon = node.attributes.icon;
                    var pid = node.pid;
                    var id = node.id;
                    var text = node.text;
                    var state = node.state;
                    var url = (node.attributes.url!=""&&node.attributes.url!=null&&node.attributes.url!="#")?node.attributes.url:"javascript:;";
                    var childNode = node.children;
                    var hasChild = (state=="0")?true:false;
                    //判断是否是文件夹
                    if (hasChild) {
                        console.log("系统不支持四级菜单");
                        break;
                    } else {
                        $('<li class="nav-3-list"><span class="icon-nav fa fa-caret-right"></span><a href="'+rootPath+'/'+url+'">'+text+'</a></li>').appendTo(parentNode.find("div.nav-3[data-id='"+pid+"']").find("ul"));
                    }
                }
            }
        }
    },
    action:{
        menuClick:function(){
            $("ul#top-menu-js").find("li.nav-list").on("mouseover",function () {
                $(".nav-2").hide();
                $(this).find(".nav-2").show();
            })
            $("div.nav-2 li.nav-2-list").on("mouseover",function () {
                $(".nav-3").hide();
                var dataId=$(this).attr("data-id");
                $("div.nav-3[data-id='"+dataId+"']").show();
            })
            $("div.nav-2").on("mouseout",function(){
                $(".nav-2").hide();
            })
        }
    },
    request:{
        requestMenuJson:function(){
            $.ajax({
                url: menu.url.urlGetMenuJson(),
                data: {},
                type: 'POST',
                dataType: "json", //可以是text，如果用text，返回的结果为字符串；如果需要json格式的，可是设置为json
                ContentType: "application/json; charset=utf-8",
                success: function (data) {
                    menu.handle.handleFirstMenuJson(data);
                },
                error: function (data) {
                    layer.msg("失败");
                }
            });
        }
    }
}